const Router = require("koa-router")
const { Success } = require("../../core/common")
const auth = require("../../middleware/auth")
const router = new Router({
    prefix: "/v1/favor"
})
const { Op } = require("sequelize");
const { User, Group } = require("../../model/index")

//关注
router.post("/favorUser", auth, async ctx => {
    const { user } = ctx.auth
    const { uid } = ctx.request.body
    const favorUser = await User.findByPk(uid)
    await user.addFollow(favorUser)
    Success(ctx, "关注成功")
})
//取消关注
router.del("/cancelFavor",auth,async ctx=>{
    const {user}=ctx.auth
    const {uid}=ctx.request.body
    const favorUser = await User.findByPk(uid)
    await user.removeFollow(favorUser)
    Success(ctx, "取消关注")
})
//获取推荐关注列表
router.get("/recommandList", auth, async ctx => {
    const { uid,user} = ctx.auth
    const { perPageCount, page } = ctx.request.query
    const offset = parseInt(perPageCount) * (parseInt(page) - 1)
    const favorIdList=(await user.getFollow()).map(item=>item.uid)
    favorIdList.push(uid)
    const { count, rows } = await User.findAndCountAll({
        where: {
            uid:{
                [Op.notIn]: favorIdList
            }
        },
        offset,
        limit: parseInt(perPageCount),
    });
 
    const list=await Promise.all(rows.map(async (item) => {
        // 等待异步操作完成，返回执行结果
        const fansCount = await item.countUser()
        return Object.assign({}, item.userInfo, {
            fansCount
        })
    }));
    Success(ctx, {
        list,
        count,
        currentPage: parseInt(page)
    })
})
//获取关注列表
router.get("/favorList",auth,async ctx=>{
    const { perPageCount, page } = ctx.request.query
    const offset = parseInt(perPageCount) * (parseInt(page) - 1)
    const {user}=ctx.auth
    const favorList=(await user.getFollow({
        offset,
        limit: parseInt(perPageCount),
    })).map(item=>item.toJSON())
    Success(ctx,{
       list:favorList,
       currentPage:parseInt(page)
    })
})
//设置分组
router.post("/setFavorGroup",auth,async ctx=>{
    const {user}=ctx.auth
    const {groups,uid}=ctx.request.body

    const favorUser=await User.findByPk(uid)

    const _userGroups=await user.getGroups()
    await Promise.all(_userGroups.map( async group => {
         const hasFollowUser= await group.hasFollowUser(favorUser)
            if(groups.includes(group.key)) {
                if(!hasFollowUser){
                    await group.addFollowUser(favorUser)
                }
            }
            else{
                if(hasFollowUser){
                    await group.removeFollowUser(favorUser)
                }
            }    
    }))

    Success(ctx,"设置分组成功")
})
//获取关注人的分组
router.get("/getFavorGroup",auth,async ctx=>{
    const {user}=ctx.auth
    const {uid}=ctx.request.query
    const followUser=await User.findByPk(uid)
    const groups=await user.getGroups({
        where:{
            type:2
        }
    })
   const groupList= await Promise.all(groups.map(async group=>{
        const _groupJson=group.toJSON()
        const hasFollowUser=await group.hasFollowUser(followUser)
        return Object.assign({},_groupJson,{hasFollowUser})
    })) 
    Success(ctx,{groupList})
})
module.exports = router